<template>
  <div
    class="header-search"
    :class="{ show: isShow }"
    id="guide-search"
      @click.stop="onShowClick"
  >
    <svg-icon
      class-name="search-icon"
      icon="search"
    ></svg-icon>
    <el-select
      ref="headerSearchSelectRef"
      class="header-search-select"
      v-model="search"
      filterable
      remote
      reserve-keyword
      :placeholder="$t('navBar.headerSearch')"
      :remote-method="remoteMethod"
      @change="onChangeSelect"
    >
      <el-option
        v-for="option,k in searchPool"
        :key="k"
        :label="option.item.title.join(' > ')"
        :value="option.item"
      >
      </el-option>
    </el-select>
  </div>
</template>

<script setup>
import { ref, watch, computed } from 'vue'
import { useRouter } from 'vue-router'
import { filterRouter } from '@/utils/route'
import { generateRoutes } from './FuseData'
import { watchSwitchLang } from '@/utils/i18n'
import Fuse from 'fuse.js'
const router = useRouter()

const searchPool = ref([])

// 搜索相关
const remoteMethod = (query) => {
  if (query === '') {
    searchPool.value = []
    return false
  }
  searchPool.value = fuse.search(query)
}
let fuseSearchPool = computed(() => {
  const routes = filterRouter(router.getRoutes())
  return generateRoutes(routes)
})

const search = ref('')
const onChangeSelect = (item) => {
  router.push(item.path)
}
let fuse
const initFuse = fuseSearchPool => {
  fuse = new Fuse(fuseSearchPool, {
    // 是否按优先级进行排序
    shouldSort: true,
    // 匹配长度超过这个值的才会被认为是匹配的
    minMatchCharLength: 1,
    // 将被搜索的键列表。 这支持嵌套路径、加权搜索、在字符串和对象数组中搜索。
    // name：搜索的键
    // weight：对应的权重
    keys: [
      {
        name: 'title',
        weight: 0.7
      },
      {
        name: 'path',
        weight: 0.3
      }
    ]
  })
}
initFuse(fuseSearchPool.value)
// 搜索框动作相关
const isShow = ref(false)
const onShowClick = () => {
  isShow.value = !isShow.value
}
const headerSearchSelectRef = ref(null)
const onClose = () => {
  headerSearchSelectRef.value.blur()
  search.value = ''
  isShow.value = false
  searchPool.value = []
}
watch(isShow, (v) => {
  if (v) {
    headerSearchSelectRef.value.focus()
    document.body.addEventListener('click', onClose)
  } else {
    document.body.removeEventListener('click', onClose)
  }
})
// 处理国际化
watchSwitchLang(() => {
  fuseSearchPool = computed(() => {
    const routes = filterRouter(router.getRoutes())
    return generateRoutes(routes)
  })
  initFuse(fuseSearchPool.value)
})
</script>

<style scoped lang="scss">
.header-search {
  font-size: 0 !important;
  :deep(.search-icon) {
    cursor: pointer;
    font-size: 18px;
    vertical-align: middle;
  }
  .header-search-select {
    font-size: 18px;
    transition: width 0.2s;
    width: 0;
    overflow: hidden;
    background: transparent;
    border-radius: 0;
    display: inline-block;
    vertical-align: middle;

    :deep(.el-input__inner) {
      border-radius: 0;
      border: 0;
      padding-left: 0;
      padding-right: 0;
      box-shadow: none !important;
      border-bottom: 1px solid #d9d9d9;
      vertical-align: middle;
    }
  }
  &.show {
    .header-search-select {
      width: 210px;
      margin-left: 10px;
    }
  }
}
</style>
